<?php

class User_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function login($username, $password) {
        $this->db->select("CID,CAcount,CPass");
        $this->db->from("customer");
        $this->db->where('CAcount = ' . "'" . $username . "'");
        $this->db->where('CPass = ' . "'" . hash("sha256", $password) . "'");
        $this->db->where("CStatus !=", 1);

        $query = $this->db->get();
        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    public function getUserToID($id) {
        $this->db->where('CID = ' . $id);
        $query = $this->db->get('customer');
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return false;
        }
    }

    public function SumKH($ostatus = null) {
        $this->db->select("CID");
        if ($ostatus != null) {
            $this->db->where("CStatus", $ostatus);
        }
        $result = $this->db->get("customer");
        if ($result->num_rows() > 0) {
            return $result->num_rows();
        } else {
            return $result = 0;
        }
    }

    public function InsertUser($qid, $cacount, $cpass, $cname, $caddress, $cemail, $cphone, $cgender, $canswer) {

        $data = array(
            'QID' => $qid,
            'CAcount' => $cacount,
            'CPass' => hash("sha256", $cpass),
            'CName' => $cname,
            'CAddress' => $caddress,
            'CEmail' => $cemail,
            'CPhone' => $cphone,
            'CGender' => $cgender,
            'Answers' => $canswer
        );
        if ($this->db->insert('customer', $data)) {
            return true;
        } else {
            return false;
        }
    }

    public function QuenMK($mang) {
        if (is_array($mang)) {
            $data = array(
                "CPass" => md5($mang["pass"])
            );
            $q = $this->db->where("CEmail", $mang["email"])->update("customer", $data);
            if ($q) {
                return TRUE;
            } else {
                return FALSE;
            }
        }
    }

    public function getQuest() {
        $query = $this->db->get('quest');
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

    public function CheckQ($Q, $email) {
        $q = $this->db->where("Answers", $Q)->where("CEmail", $email)->get("customer");
        if ($q->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function check_user_reg($username) {
        $this->db->where('CAcount =' . "'" . $username . "'");
        $query = $this->db->get('customer');
        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function GetAllKh($name = NULL, $limit, $offset) {
        $q = $this->db->select('CID,CAcount,CPass,CName,CStatus,CAddress,CEmail,CPhone,CGender,Answers')
                ->from('customer')
                ->limit($limit, $offset);
        if ($name != NULL) {
            $q->like('CAcount', $name);
        }
        $ret["rows"] = $q->get();

        $q = $this->db->select('count(*) as count', FALSE)->from("customer");
        if ($name != NULL) {
            $q->like('CAcount', $name);
        }
        $tmp = $q->get()->result();
        $ret["num_rows"] = $tmp[0]->count;
        return $ret;
    }

    //xoa khach hang
    public function DeleteKH($cid) {
        $q = $this->db->where("CID", $cid)
                ->delete("customer");
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function UpdateStt($cid, $stt) {
        $data = array(
            "CStatus" => $stt
        );
        $q = $this->db->where("CID", $cid)->update("customer", $data);
        if ($q) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function GetEmail($email) {
        $q = $this->db->where("CEmail", $email)->get("customer");
        if ($q->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>
